package com.situ.lession1226.api;

import com.github.pagehelper.PageInfo;
import com.situ.lession1226.model.News;
import com.situ.lession1226.model.News;
import com.situ.lession1226.service.NewsService;
import com.situ.lession1226.service.NewsService;
import com.situ.lession1226.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 * 电影的增删改查
 */
@RestController
@RequestMapping(value = "/api/v1/news", produces = "application/json;charset=utf-8")
public class NewsApi {
    private NewsService newsService;

    @Autowired
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    /**
     *
     */
    @GetMapping
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                     News news) {
        //创建一个分页对象
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);

        List<News> Newss = newsService.findAll(pi, news);

        //PageHelper的分页对象
        PageInfo<?> pif = pi.getPageInfo();

        return Map.of("news", Newss, "success", true, "pi", pif);
    }


    /**
     *
     */
    @DeleteMapping
    public Map<String, Object> deleteByIds(Integer[] ids) {
        int rows = newsService.deleteByIds(ids);//受影响的行数
        return Map.of("success", true, "rows", rows);
    }

    /**
     *
     */
    @PostMapping
    public Map<String, Object> save(News news) {
        boolean success = newsService.save(news);
        return Map.of("success", success);
    }

    /**
     *
     */
    @PatchMapping
    public Map<String, Object> update(News news) {
        boolean success = newsService.update(news);
        return Map.of("success", success);
    }


}
